<!--
 * @Author: Rachel 943701297@qq.com
 * @Date: 2022-07-14 08:57:16
 * @LastEditors: Rachel 943701297@qq.com
 * @LastEditTime: 2022-07-14 09:09:55
 * @FilePath: \JS\java-script-learning-notes\JSCode\切图.html
 * @Description: 
 * 
 * Copyright (c) 2022 by Rachel 943701297@qq.com, All Rights Reserved. 
-->
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>切图</title>
  </head>
  <body>
    <div>
      <img src="img/1.jpg" />
      <br />
      <button id="pre">上一张</button>
      <button id="next">下一张</button>
      <button id="start">开始</button>
      <button id="stop">结束</button>
    </div>
    <script>
      let img = document.getElementsByTagName("img")[0];
      let pre = document.getElementById("pre");
      let next = document.getElementById("next");
      let start = document.getElementById("start");
      let stop = document.getElementById("stop");
      let timer;
      let imgArr = [
        "img/1.jpg",
        "img/2.jpg",
        "img/3.jpg",
        "img/4.jpg",
        "img/5.jpg",
      ];
      let index = 0;

      start.onclick = () => {
        window.clearInterval(timer);
        timer = window.setInterval(() => {
          img.src = imgArr[index];
          index++;
          index %= imgArr.length;
        }, 1000);
      };
      stop.onclick = () => {
        window.clearInterval(timer);
      };
    </script>
  </body>
</html>
